home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Environments / SmallEiffel 0.3.3 / SmallEiffel PPC / lib_std / array2.e < prev    next >
Encoding:
Text File  |  1996-06-13  |  6.8 KB  |  404 lines  |  [TEXT/EDIT]

  1. -- Part of SmallEiffel -- Read DISCLAIMER file -- Copyright (C) 
  2. -- Dominique COLNET and Suzanne COLLIN -- colnet@loria.fr
  3. --
  4. class ARRAY2[G]
  5.    
  6. inherit 
  7.    ANY
  8.       redefine copy, is_equal, fill_tagged_out_memory
  9.       end;
  10.    
  11. creation make, array2
  12.  
  13. feature {ARRAY2}
  14.    
  15.    storage: ARRAY[ARRAY[G]];
  16.    
  17. feature 
  18.    
  19.    make(l1, u1, l2, u2: INTEGER) is
  20.       require
  21.      l1 <= u1;
  22.      l2 <= u2;
  23.       local
  24.      i: INTEGER;
  25.      sa: ARRAY[G];
  26.       do
  27.      from  
  28.         !!storage.make(l1,u1);
  29.         i := storage.lower;
  30.      until
  31.         i > storage.upper
  32.      loop
  33.         !!sa.make(l2,u2);
  34.         storage.put(sa,i);
  35.         i := i + 1;
  36.      end;
  37.       ensure
  38.      lower1 = l1;
  39.      upper1 = u1;
  40.      lower2 = l2;
  41.      upper2 = u2;
  42.       end;
  43.    
  44.    array2(s: ARRAY[ARRAY[G]]) is
  45.       require
  46.      s /= Void;
  47.      s.count > 0;
  48.       local
  49.      i, l2, u2: INTEGER;
  50.       do
  51.      storage := s;
  52.      debug
  53.         from  
  54.            i := storage.lower + 1;
  55.         until
  56.            i > storage.upper
  57.         loop
  58.            if lower2 /= storage.item(i).lower then
  59.           std_error.put_string("ARRAY2: Bad `lower2' at line ")
  60.           std_error.put_integer(i);
  61.           std_error.put_new_line;
  62.           crash;
  63.            elseif upper2 /= storage.item(i).upper then
  64.           std_error.put_string("ARRAY2: Bad `upper2' at line ")
  65.           std_error.put_integer(i);
  66.           std_error.put_new_line;
  67.           crash;
  68.            end;
  69.            i := i + 1;
  70.         end;
  71.      end;
  72.       ensure
  73.      storage = s;
  74.       end;
  75.    
  76. feature 
  77.    
  78.    lower1: INTEGER is
  79.       do
  80.      Result := storage.lower;
  81.       end;
  82.    
  83.    upper1: INTEGER is
  84.       do
  85.      Result := storage.upper;
  86.       end;
  87.    
  88.    count1: INTEGER is
  89.       do
  90.      Result := storage.count;
  91.       end;
  92.    
  93.    lower2: INTEGER is
  94.       do
  95.      Result := storage.first.lower;
  96.       end;
  97.    
  98.    upper2: INTEGER is
  99.       do
  100.      Result := storage.first.upper;
  101.       end;
  102.    
  103.    count2: INTEGER is
  104.       do
  105.      Result := storage.first.count;
  106.       end;
  107.    
  108.    count: INTEGER is
  109.       do
  110.      Result := count1 * count2;
  111.       end;
  112.    
  113.    valid_index(i, j: INTEGER): BOOLEAN is
  114.       do
  115.      Result := (lower1 <= i and then
  116.             i <= upper1 and then
  117.             lower2 <= j and then
  118.             j <= upper2); 
  119.       end;
  120.       
  121.    item(i, j: INTEGER): G is
  122.       require
  123.      valid_index(i,j);
  124.       do
  125.      Result := storage.item(i).item(j);
  126.       end;
  127.    
  128.    is_equal(other: like Current): BOOLEAN is
  129.       local
  130.      i: INTEGER;
  131.       do
  132.      if Current = other then
  133.         Result := true;
  134.      elseif lower1 /= other.lower1 then
  135.      elseif upper1 /= other.upper1 then
  136.      elseif lower2 /= other.lower2 then
  137.      elseif upper2 /= other.upper2 then
  138.      else
  139.         from
  140.            Result := true;
  141.            i := storage.lower;
  142.         until
  143.            not Result or else i > storage.upper
  144.         loop
  145.            Result := storage.item(i).is_equal(other.storage.item(i));
  146.            i := i + 1;
  147.         end;
  148.      end;
  149.       end;
  150.    
  151. feature 
  152.    
  153.    put(x: G; i, j: INTEGER) is
  154.       require
  155.      valid_index(i,j);
  156.       do
  157.      storage.item(i).put(x,j);     
  158.       end;
  159.    
  160.    swap(i1, j1, i2, j2: INTEGER) is
  161.       require
  162.      valid_index (i1,j1);
  163.      valid_index (i2,j2);
  164.       local
  165.      tmp: G;
  166.       do
  167.      tmp := item(i1,j1);
  168.      put(item(i2,j2),i1,j1);
  169.      put(tmp,i2,j2);
  170.       end;
  171.  
  172.    copy(other: like Current) is
  173.       local
  174.      i: INTEGER;
  175.       do
  176.      make(other.lower1,other.upper1,other.lower2,other.upper2);
  177.      from  
  178.         i := other.lower1;
  179.         i := storage.lower;
  180.      until
  181.         i > storage.upper
  182.      loop
  183.         storage.item(i).copy(other.storage.item(i));
  184.         i := i + 1;
  185.      end;
  186.       end;
  187.    
  188. feature 
  189.  
  190.    set_all_with(x: G) is
  191.       local 
  192.          i: INTEGER;
  193.       do
  194.      from 
  195.         i := storage.lower
  196.      until
  197.         i > storage.upper
  198.          loop
  199.             storage.item(i).set_all_with(x)
  200.         i := i + 1
  201.          end;
  202.       end;
  203.  
  204.    nb_occurrences(elt: G): INTEGER is
  205.      -- Number of occurrences using `equal'.
  206.      -- See also `fast_nb_occurrences' to chose
  207.      -- the apropriate one.
  208.       local
  209.      i: INTEGER;
  210.       do
  211.      from  
  212.         i := lower1;
  213.      until
  214.         i > upper1
  215.      loop
  216.         Result := Result + storage.item(i).nb_occurrences(elt);
  217.         i := i + 1;
  218.      end;
  219.       ensure
  220.      Result >= 0;
  221.       end;
  222.       
  223.    fast_nb_occurrences(elt: G): INTEGER is
  224.      -- Number of occurrences using `='.
  225.       local
  226.      i: INTEGER;
  227.       do
  228.      from  
  229.         i := lower1;
  230.      until
  231.         i > upper1
  232.      loop
  233.         Result := Result + storage.item(i).fast_nb_occurrences(elt);
  234.         i := i + 1;
  235.      end;
  236.       ensure
  237.      Result >= 0;
  238.       end;
  239.  
  240. feature -- Resizing :
  241.  
  242.    resize(l1, u1, l2, u2: INTEGER) is
  243.       require
  244.      u1 >= l1 - 1;
  245.      u2 >= l2 - 1;
  246.       local
  247.      i: INTEGER;
  248.      oldl1, oldu1: INTEGER;
  249.      tmp: ARRAY[G];
  250.       do
  251.      oldl1 := lower1;
  252.      oldu1 := upper1;
  253.      storage.resize (l1,u1); 
  254.      from
  255.         i := l1;
  256.      until
  257.         i > u1
  258.      loop
  259.         if i < oldl1 or i > oldu1 then
  260.            !!tmp.make (lower2, upper2);
  261.            storage.put (tmp, i)
  262.         else
  263.            storage.item (i).resize (l2,u2);
  264.         end;
  265.         i := i + 1;
  266.      end;
  267.       ensure
  268.      lower1 = l1;
  269.      upper1 = u1;
  270.      lower2 = l2;
  271.      upper2 = u2;
  272.       end;
  273.  
  274.    resize1(l, u: INTEGER) is
  275.       require
  276.      u >= l - 1;
  277.       local
  278.      i, oldl, oldc: INTEGER;
  279.      tmp: ARRAY[G];
  280.       do
  281.      oldl := lower1;
  282.      oldc := upper1;
  283.      storage.resize (l,u);
  284.      if l < oldl then
  285.         from
  286.            i := l;
  287.         until
  288.            i = lower1
  289.         loop
  290.            !!tmp.make (lower2, upper2);
  291.            storage.put (tmp, i)
  292.            i := i + 1;
  293.         end;
  294.      end;
  295.      if u > oldc then
  296.         from
  297.            i := oldc + 1;
  298.         until
  299.            i > u
  300.         loop
  301.            !!tmp.make (lower2, upper2);
  302.            storage.put (tmp, i)
  303.            i := i + 1;
  304.         end;
  305.      end;
  306.       end;
  307.    
  308.    resize2 (l, u : INTEGER) is
  309.       require
  310.      u > l - 1;
  311.       local
  312.      i : INTEGER;
  313.       do
  314.      from
  315.         i := lower1;
  316.      until
  317.         i > upper1
  318.      loop
  319.         storage.item (i).resize (l,u);
  320.         i := i + 1;
  321.      end;
  322.       ensure
  323.      lower2 = l;
  324.      upper2 = u;
  325.       end;
  326.    
  327.  
  328.  
  329. feature -- Other features :
  330.       
  331.    replace_all(x, r: G) is
  332.       local
  333.      i: INTEGER;
  334.       do
  335.      from 
  336.         i := lower1;
  337.      until
  338.         i > upper1
  339.      loop
  340.         storage.item(i).replace_all(x,r);
  341.         i := i + 1;
  342.      end;
  343.       end;
  344.    
  345.    fast_replace_all(x, r: G) is
  346.       local
  347.      i: INTEGER;
  348.       do
  349.      from 
  350.         i := lower1;
  351.      until
  352.         i > upper1
  353.      loop
  354.         storage.item(i).fast_replace_all(x,r);
  355.         i := i + 1;
  356.      end;
  357.       end;
  358.      
  359.    transpose is
  360.       local
  361.      i,j : INTEGER;
  362.      oldc1, oldc2 : INTEGER;
  363.       do
  364.      oldc1 := count1;
  365.      oldc2 := count2;
  366.      if count1 > count2 then
  367.         resize2 (lower2, lower2 + count1 -1);
  368.      elseif count2 > count1 then
  369.         resize1 (lower1, lower1 + count2 - 1);
  370.      end;
  371.      from  
  372.         i := lower1;
  373.      until 
  374.         i > upper1 - 1
  375.      loop
  376.         from  
  377.            j := i + 1;
  378.         until 
  379.            j > upper2
  380.         loop
  381.            swap(i,j,j,i);
  382.            j := j + 1;
  383.         end;
  384.         i := i + 1;
  385.      end;
  386.      resize(lower1,lower1 + oldc2 - 1,lower2,lower2 + oldc1 - 1);
  387.       ensure
  388.      count = oldc1 * oldc2;     
  389.       end;
  390.  
  391. feature -- Object Printing :
  392.  
  393.    fill_tagged_out_memory is
  394.       do
  395.      tagged_out_memory.append("storage: "); 
  396.      storage.out_in_tagged_out_memory;
  397.       end;
  398.  
  399. invariant
  400.    
  401.    storage.count >= 1;
  402.    
  403. end -- ARRAY2
  404.